//
//  SessionViewController.m
//  MyProject
//
//  Created by yun on 2018/4/20.
//  Copyright © 2018年 yun. All rights reserved.
//

#import "SessionViewController.h"
#import "BackgroundDownloadManager.h"

@interface SessionViewController ()

@property (nonatomic,strong) NSURLSession *session;
@property (nonatomic,strong) NSURLSessionDownloadTask *loadTask;

@property (nonatomic,strong) NSMutableData *receiveData;
@property (nonatomic,strong) NSData *loadData;
@property (nonatomic,strong) UIImageView *imgv;

@end

static NSString * const tempUrl = @"http://ip.taobao.com/service/getIpInfo.php?ip=myip";

@implementation SessionViewController

/*
 NSJSONReadingOptions:

 NSJSONReadingMutableContainers = (1UL << 0),// 指定将数组和字典创建为可变对象。
 NSJSONReadingMutableLeaves = (1UL << 1),// 指定将JSON对象图中的子字符串创建为NSMutableString的实例。
 NSJSONReadingAllowFragments = (1UL << 2)// 指定解析器应允许不是NSArray或NSDictionary实例的顶级对象。
 
 */

- (void)dealloc{
    NSLog(@"%s注销",__func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemClick)];
    
    UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
    downButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [downButton setTitle:@"下载" forState:(UIControlStateNormal)];
    [downButton setTitleColor:kHexColor(0x87CEEB) forState:UIControlStateNormal];
    [downButton addTarget:self action:@selector(downButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:downButton];
    [downButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
    
    UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    pauseButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [pauseButton setTitle:@"暂停" forState:(UIControlStateNormal)];
    [pauseButton setTitleColor:kHexColor(0x87CEEB) forState:UIControlStateNormal];
    [pauseButton addTarget:self action:@selector(pauseButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:pauseButton];
    [pauseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(downButton);
        make.right.equalTo(downButton.mas_left).offset(-20);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
    
    UIButton *continueButton = [UIButton buttonWithType:UIButtonTypeCustom];
    continueButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [continueButton setTitle:@"继续下载" forState:(UIControlStateNormal)];
    [continueButton setTitleColor:kHexColor(0x87CEEB) forState:UIControlStateNormal];
    [continueButton addTarget:self action:@selector(continueButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:continueButton];
    [continueButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(downButton);
        make.left.equalTo(downButton.mas_right).offset(20);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
    
    
}

- (void)leftBarButtonItemClick{
//    [self.session finishTasksAndInvalidate];
    [self.navigationController popViewControllerAnimated:YES];
}

// 相关按钮
- (void)downButtonClick:(UIButton *)sender{
    
    [[BackgroundDownloadManager sharedBackgroundDownloadManager] startDownloadTaskWithURL:@"http://sw.bos.baidu.com/sw-search-sp/software/bd15545b3c092/QQ_mac_6.3.0.dmg"];
}

- (void)pauseButtonClick:(UIButton *)sender{
    [[BackgroundDownloadManager sharedBackgroundDownloadManager] cancelDownloadTask];
}

- (void)continueButtonClick:(UIButton *)sender{
    
    [[BackgroundDownloadManager sharedBackgroundDownloadManager] continueDownloadTaskWithUrl:@"http://sw.bos.baidu.com/sw-search-sp/software/bd15545b3c092/QQ_mac_6.3.0.dmg"];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[BackgroundDownloadManager sharedBackgroundDownloadManager] checkFileExist:@"http://sw.bos.baidu.com/sw-search-sp/software/bd15545b3c092/QQ_mac_6.3.0.dmg"];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
